using System.Collections;
using ExitGames.Client.Photon;
using Photon;
using UnityEngine;
using UnityEngine.UI;

public class Manager : Photon.MonoBehaviour
{
	[HideInInspector]
	public PlayerSetup s_PlayerSetup;

	public bool single;

	[HideInInspector]
	public SaveAndLoad s_SaveAndLoad;

	public int currentPlayerskin;

	public GameObject[] m_MyVechiles = new GameObject[2];

	public Transform[] SpawnPosition;

	public static Manager Instance { get; private set; }

	private void Awake()
	{
		Instance = this;
		s_SaveAndLoad = GetComponent<SaveAndLoad>();
		single = SavesAllFunctions.Local;
		Debug.Log("LOcal " + SavesAllFunctions.Local);
	}

	private void Start()
	{
		if (!single)
		{
			if (PhotonNetwork.room != null && !PhotonNetwork.offlineMode)
			{
				SpawnPlayer("Player1");
			}
			else
			{
				PhotonNetwork.autoJoinLobby = true;
				PhotonNetwork.ConnectUsingSettings("1.0.0.3");
			}
		}
		else
		{
			SpawnPlayer("Player1");
		}
		Cursor.lockState = CursorLockMode.None;
		Cursor.visible = true;
		if (PlayerPrefs.HasKey("QualitySetting"))
		{
			QualitySettings.SetQualityLevel(PlayerPrefs.GetInt("QualitySetting"));
		}
		if (PlayerPrefs.HasKey("LoadMap") && PlayerPrefs.GetString("LoadMap") != string.Empty)
		{
			s_SaveAndLoad.Load(PlayerPrefs.GetString("LoadMap"));
		}
	}

	private void OnJoinedLobby()
	{
		RoomOptions roomOptions = new RoomOptions();
		roomOptions.maxPlayers = 1;
		roomOptions.customRoomProperties = new ExitGames.Client.Photon.Hashtable();
		roomOptions.customRoomProperties.Add("MaxProps", 250);
		roomOptions.customRoomProperties.Add("CurrentProps", 0);
		PhotonNetwork.JoinOrCreateRoom("Kresh1", roomOptions, TypedLobby.Default);
	}

	public void SpawnPlayer(string NameObject)
	{
		currentPlayerskin = 0;
		bool flag = false;
		if (PlayerPrefs.HasKey("ADMIN") && PlayerPrefs.GetInt("ADMIN") == 1)
		{
			flag = true;
		}
		if (PlayerPrefs.HasKey("CurrentPlayer"))
		{
			currentPlayerskin = PlayerPrefs.GetInt("CurrentPlayer");
		}
		GameObject gameObject = null;
		Object.Destroy(Camera.main.gameObject);
		gameObject = ((PhotonNetwork.room == null || PhotonNetwork.offlineMode) ? (Object.Instantiate(Resources.Load<GameObject>("Player1"), GetRandomPlayerPosition(), Quaternion.identity) as GameObject) : PhotonNetwork.Instantiate(NameObject, GetRandomPlayerPosition(), Quaternion.identity, 0, new object[2] { currentPlayerskin, flag }));
		s_PlayerSetup = gameObject.GetComponent<PlayerSetup>();
		StartCoroutine(MoneyTimeGive());
	}

	private void OnJoinedRoom()
	{
		if (PlayerPrefs.HasKey("NamePlayer"))
		{
			string text = PlayerPrefs.GetString("NamePlayer");
			if (text == string.Empty)
			{
				text = "noname" + Random.Range(1, 1000000);
			}
			PhotonNetwork.playerName = text;
		}
		StartCoroutine("SpawnPlayer", "Player1");
	}

	public Vector3 GetRandomPlayerPosition()
	{
		int num = Random.Range(0, SpawnPosition.Length);
		Vector3 vector = new Vector3(Random.Range(2.5f, 4f) * (float)((Random.Range(0, 2) != 1) ? 1 : (-1)), 2f, Random.Range(2.5f, 4f) * (float)((Random.Range(0, 2) != 1) ? 1 : (-1)));
		Vector3 result = new Vector3(SpawnPosition[num].transform.position.x + vector.x, SpawnPosition[num].transform.position.y + vector.y, SpawnPosition[num].transform.position.z + vector.z);
		return result;
	}

	private void Update()
	{
	}

	public void ChatMessage(string playerName, string textM, bool admin)
	{
		if (!(textM == string.Empty) && s_PlayerSetup != null)
		{
			s_PlayerSetup.s_PlayerOthers.CheckMessage();
			GameObject gameObject = Object.Instantiate(s_PlayerSetup.s_PlayerOthers.m_SpawnTextMessage, s_PlayerSetup.s_PlayerOthers.SpawnPosition.transform.position, Quaternion.identity) as GameObject;
			gameObject.transform.SetParent(s_PlayerSetup.s_PlayerOthers.SpawnPosition.transform);
			gameObject.transform.localPosition = new Vector3(1f, 1f, 1f);
			gameObject.transform.localScale = new Vector3(1f, 1f, 1f);
			if (admin)
			{
				gameObject.GetComponent<Text>().text = "<color=#" + ColorUtility.ToHtmlStringRGB(Color.red) + ">[ADMIN]" + playerName + "</color> : " + textM;
			}
			else
			{
				gameObject.GetComponent<Text>().text = "<color=#" + ColorUtility.ToHtmlStringRGB(Color.red) + ">" + playerName + "</color> : " + textM;
			}
			s_PlayerSetup.s_PlayerOthers.AllMessageInChat.Add(gameObject);
			s_PlayerSetup.s_PlayerOthers.c_CanvasPlayer.ChatSliderVal.value = 0f;
			GameObject gameObject2 = Object.Instantiate(s_PlayerSetup.s_PlayerOthers.m_SpawnTextMessage, s_PlayerSetup.s_PlayerOthers.SpawnPositionLocal.transform.position, Quaternion.identity) as GameObject;
			gameObject2.transform.SetParent(s_PlayerSetup.s_PlayerOthers.SpawnPositionLocal.transform);
			gameObject2.transform.localPosition = new Vector3(1f, 1f, 1f);
			gameObject2.transform.localScale = new Vector3(1f, 1f, 1f);
			if (admin)
			{
				gameObject2.GetComponent<Text>().text = "<color=#" + ColorUtility.ToHtmlStringRGB(Color.red) + ">[ADMIN]" + playerName + "</color> : " + textM;
			}
			else
			{
				gameObject2.GetComponent<Text>().text = "<color=#" + ColorUtility.ToHtmlStringRGB(Color.red) + ">" + playerName + "</color> : " + textM;
			}
			s_PlayerSetup.s_PlayerOthers.AllMessageInLocalChat.Add(gameObject2);
			StartCoroutine(DeleteMessage(gameObject2));
			s_PlayerSetup.s_PlayerOthers.c_CanvasPlayer.ChatSliderVal.value = 0f;
		}
	}

	private IEnumerator DeleteMessage(GameObject message)
	{
		yield return new WaitForSeconds(10f);
		Object.Destroy(message);
	}

	private void OnDisconnectedFromPhoton()
	{
		Application.LoadLevel(1);
	}

	private IEnumerator MoneyTimeGive()
	{
		while (true)
		{
			yield return new WaitForSeconds(60f);
			if (PlayerPrefs.HasKey("Coins"))
			{
				int money = PlayerPrefs.GetInt("Coins");
				PlayerPrefs.SetInt("Coins", money + 1250);
			}
			else
			{
				PlayerPrefs.SetInt("Coins", 1250);
			}
		}
	}
}
